package com.google.code.swingonfire.showcase;

import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;

import com.google.code.swingonfire.builder.JLabelBuilder;

public class ShowcaseListCellRenderer implements ListCellRenderer
{

    private final JLabel label;
    private final Font defaultFont;
    private final Border defaultBorder;
    private final Font topicFont;
    private final Border topicBorder;

    public ShowcaseListCellRenderer()
    {
        super();

        label = new JLabelBuilder().opaque(true).build();

        defaultFont = label.getFont();
        defaultBorder = BorderFactory.createEmptyBorder(4, 24, 4, 8);
        topicFont = defaultFont.deriveFont(Font.BOLD, defaultFont.getSize() * 1.6f);
        topicBorder = BorderFactory.createEmptyBorder(4, 8, 4, 8);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
        boolean cellHasFocus)
    {
        ShowcaseSamples sample = (ShowcaseSamples) value;

        label.setText(sample.getName());

        if (sample.isTopic())
        {
            label.setBorder(topicBorder);
            label.setFont(topicFont);
        }
        else
        {
            label.setBorder(defaultBorder);
            label.setFont(defaultFont);
        }

        if (isSelected) {
            label.setForeground(list.getSelectionForeground());
            label.setBackground(list.getSelectionBackground());
        }
        else {
            label.setForeground(list.getForeground());
            label.setBackground(list.getBackground());
        }
        
        return label;
    }
}
